package com.shioirikukuri.service.impl;

import com.shioirikukuri.domain.OperateIntegralVo;
import com.shioirikukuri.mapper.UsableIntegralMapper;
import com.shioirikukuri.service.IUsableIntegralServiceTCC;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UsableIntegralServiceTCCImpl implements IUsableIntegralServiceTCC {
    @Autowired
    private UsableIntegralMapper usableIntegralMapper;

    @Override
    public Boolean payTry(OperateIntegralVo vo, BusinessActionContext context) {
        System.out.println("执行payTry逻辑");
        int effectCount = usableIntegralMapper.freezeIntegral(vo.getPhone(), vo.getValue());
        return effectCount>0;
    }

    @Override
    public void payConfirm(BusinessActionContext context) {
        System.out.println("执行payConfirm逻辑");
        OperateIntegralVo vo = context.getActionContext("vo",OperateIntegralVo.class);
        usableIntegralMapper.commitChange(vo.getPhone(), vo.getValue());
    }

    @Override
    public void payCancel(BusinessActionContext context) {
        System.out.println("执行payCancel逻辑");
        OperateIntegralVo vo = context.getActionContext("vo",OperateIntegralVo.class);
        usableIntegralMapper.unFreezeIntegral(vo.getPhone(), vo.getValue());

    }


    @Override
    public void refundTry(OperateIntegralVo vo, BusinessActionContext context) {
        //退款时不在Try中预留资源
        System.out.println("refundTry执行");

    }

    @Override
    public void refundConfirm(BusinessActionContext context) {
        //在这里退款积分
        System.out.println("refundConfirm执行");
        OperateIntegralVo vo = context.getActionContext("vo",OperateIntegralVo.class);
        usableIntegralMapper.incrIntegral(vo.getPhone(), vo.getValue());

    }

    @Override
    public void refundCancel(BusinessActionContext context) {
        //由于没有预留Try中资源,无需回滚
        System.out.println("refundCancel执行");

    }
}
